﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Template.Web
{
    public partial class PageDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                int uid = 0, tid = 0, pageid = 0;
                try
                {
                    uid = Int32.Parse(Request["uid"]);
                }
                catch
                {

                }
                try
                {
                    tid = Int32.Parse(Request["tid"]);
                }
                catch
                {

                }
                try
                {
                    pageid = Int32.Parse(Request["pid"]);
                }
                catch
                {

                }
                CmsPageInfo page = null;
                CmsUserTemplatePage temppage = null;
                CmsUserTemplateInfo temp = null;
                IList<CmsUserTemplatePage> pageList = null;
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        temppage = session.Get<CmsUserTemplatePage>(uid);
                    }
                    if (tid > 0)
                    {
                        temp = session.Get<CmsUserTemplateInfo>(tid);
                    }
                    if (pageid > 0)
                    {
                        page = session.Get<CmsPageInfo>(pageid);
                    }
                    if ((temppage == null && temp == null) || temp.OwnerAccount != UserKit.GetUserAccountWithoutException())
                    {
                        new FrameHelper(this).Alert(new ReturnCode()
                        {
                            statusCode = 300,
                            message = "参数错误"
                        });
                        return;
                    }
                    if (temppage != null)
                    {
                        ViewState["uid"] = uid;
                        ViewState["tid"] = temppage.UserTempId;
                    }
                    else
                    {
                        ViewState["tid"] = tid;
                        temppage = new CmsUserTemplatePage();
                    }

                    pageList = session.CreateQuery("from CmsUserTemplatePage where UserTempId=:tid order by PageLevel,Sort")
                        .SetParameter("tid", ViewState["tid"])
                        .List<CmsUserTemplatePage>();
                    pageList.Insert(0, new CmsUserTemplatePage() { Id = -1, Name = "--请选择--" });
                }
                if (page != null)
                {
                    temppage.DocKind = page.DocKind;
                    temppage.FileUrl = page.FileUrl;
                    temppage.ImageUrl = page.ImageUrl;
                    temppage.Kind = page.Kind;
                    temppage.Memo = page.Memo;
                    temppage.Name = page.Name;
                    temppage.PreviewUrl = page.PreviewUrl;
                    temppage.Status = page.Status;
                    temppage.TemplateData = page.TemplateData;
                    temppage.ThumbnailUrl = page.ThumbnailUrl;
                }
                name.Text = temppage.Name;
                sort.Text = temppage.Sort.ToString();
                memo.Text = temppage.Memo;
                template1.Value = temppage.TemplateData;
                previewurl.Value = temppage.PreviewUrl;
                thumburl.Value = temppage.ThumbnailUrl;
                imgurl.Value = temppage.ImageUrl;


                nextpage.DataSource = pageList;
                nextpage.DataValueField = "Id";
                nextpage.DataTextField = "Name";
                nextpage.DataBind();
                nextpage.SelectedValue = temppage.NextUtpId == 0 ? "-1" : temppage.NextUtpId.ToString();


                ParamKit.Bind("template_pages_level", level, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, temppage.PageLevel, "-1");

                ParamKit.Bind("template_pages_kind", kind, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, temppage.Kind, "-1");
            }
            else
            {
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                int tid = ViewState["tid"] == null ? 0 : (int)ViewState["tid"];

                CmsUserTemplatePage page = null;
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    CmsUserTemplateInfo temp = session.Get<CmsUserTemplateInfo>(tid);

                    if (temp.OwnerAccount != UserKit.GetUserAccountWithoutException())
                    {
                        new FrameHelper(this).Alert(new ReturnCode()
                        {
                            statusCode = 300,
                            message = "参数错误"
                        });
                        return;
                    }

                    page = session.Get<CmsUserTemplatePage>(uid);
                    if (page == null)
                    {
                        page = new CmsUserTemplatePage();
                        page.UserTempId = tid;
                    }

                    page.Name = name.Text;
                    try
                    {
                        page.Sort = Int32.Parse(sort.Text);
                    }
                    catch
                    {

                    }
                    page.PageLevel = level.SelectedValue;
                    page.ThumbnailUrl = thumburl.Value;
                    page.ImageUrl = imgurl.Value;
                    page.Memo = memo.Text;
                    page.TemplateData = template1.Value;
                    page.Kind = kind.SelectedValue;
                    page.DocKind = "Template";
                    page.PreviewUrl = previewurl.Value;
                    page.NextUtpId = Int32.Parse(nextpage.SelectedValue);
                    session.SaveOrUpdate(page);
                    session.Flush();
                }


                FrameHelper _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Template/Web/PageDetail.aspx?tabid=PageDetail&tid=" + page.UserTempId;

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + page.Id;
                }

                _framePage.Alert(rc);
            }

        }
    }
}